﻿using EmitMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Core;
using WebSite.Data.NH.Repositories;
using WebSite.Model;
using WebSite.Web.Models;
using WebSite.Web.SessionAndFilter;

namespace WebSite.Web.Controllers.Admin
{
    [AuthorAttribute]
    public class RecruitmentController : Controller
    { 
        private readonly IRecruitmentRepository rr;

        public RecruitmentController(IRecruitmentRepository rr)
        {
            this.rr = rr;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(int Id = 0)
        {
            RecruitmentVM rv = null;
            if (Id == 0)
            {
                rv = new RecruitmentVM();
                rv.CompanyId = Sessions.User.CompanyId;
            }
            else
            {
                ObjectsMapper<RecruitmentInfo, RecruitmentVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<RecruitmentInfo, RecruitmentVM>();
                RecruitmentInfo ri = rr.GetRec(Id);
                rv = mapper.Map(ri);
            }
            return View(rv);
        }

        [HttpPost]
        public ActionResult Edit(RecruitmentVM rv)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<RecruitmentVM, RecruitmentInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<RecruitmentVM, RecruitmentInfo>();

                var user = Sessions.User;

                if (rv.Id == 0)
                {
                    var a = mapper.Map(rv);
                    a.ReleaseDate = DateTime.Now;
                    bool res = rr.AddRec(a);
                    return Json(res);
                }
                else
                {
                    var a = mapper.Map(rv);
                    bool res = rr.UpdateRec(a);
                    return Json(res);
                }

            }
            return View();
        }


        [HttpPost]
        public JsonResult Recruitments(string positionfunction, int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;

            GridData<RecruitmentVM> data = new GridData<RecruitmentVM>();

            ObjectsMapper<RecruitmentInfo, RecruitmentVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<RecruitmentInfo, RecruitmentVM>();

            var where = PredicateExtension.True<RecruitmentInfo>();
            where = where.AndAlso(u => u.CompanyId == user.CompanyId);
            if (!string.IsNullOrEmpty(positionfunction))
            {
                where = where.AndAlso(u => u.Positionfunction.Contains(positionfunction.Trim()));
            }

            var count = rr.LoadCount(where);
            var list = rr.LoadAll(where, page, pageSize, new OrderByExpression<RecruitmentInfo, int>(u => u.Id, true));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }

        [HttpPost]
        public ActionResult DeleteRecruitment(int id)
        {
            var b = false;
            b = rr.DeleteRec(id);
            return Json(b);
        }

    }
}
